package gui.input.engineer;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.beans.PojoObservables;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.observable.map.IObservableMap;
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.wb.swt.SWTResourceManager;

import data.model.Developer;
import data.model.ProjectsCollection;

public class EditDataDialog extends Dialog {
	private static final Developer defaultSelectedDeveloper = null;

	@SuppressWarnings("unused")
	private DataBindingContext m_bindingContext;
	private Label lblDeveloper;
	private ComboViewer comboDeveloperViewer;
	private EditActualEffortComposite editActualEffortComposite;
	private EditEstimatedEffortComposite editEstimatedEffortComposite;
	private final Composite f_composite;
	private final ProjectsCollection projects;
	private Developer selectedDeveloper;
	private TabFolder tabFolder;

	/**
	 * Create the dialog.
	 * 
	 * @param parentShell
	 * @param projects
	 * @throws IllegalArgumentException
	 *             falls projects null ist
	 * @wbp.parser.constructor
	 */
	public EditDataDialog(Composite composite, Shell parentShell,
			ProjectsCollection projects) throws IllegalArgumentException {

		this(composite, parentShell, projects, defaultSelectedDeveloper);
	}

	/**
	 * Create the dialog.
	 * 
	 * @param parentShell
	 * @param projects
	 * @param developer
	 * @throws IllegalArgumentException
	 *             falls projects null ist
	 */
	public EditDataDialog(Composite composite, Shell parentShell,
			ProjectsCollection projects, Developer developer)
			throws IllegalArgumentException {
		super(parentShell);
		if (projects == null) {
			throw new IllegalArgumentException("projects must not be null");
		}
		this.projects = projects;
		this.selectedDeveloper = developer;
		f_composite = composite;
	}

	/**
	 * Create contents of the dialog.
	 * 
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		GridLayout gridLayout = (GridLayout) container.getLayout();
		gridLayout.numColumns = 3;

		// Entwickler
		lblDeveloper = new Label(container, SWT.NONE);
		lblDeveloper.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblDeveloper.setText("Developer:");

		comboDeveloperViewer = new ComboViewer(container, SWT.NONE);
		comboDeveloperViewer
				.addPostSelectionChangedListener(new ISelectionChangedListener() {
					@Override
					public void selectionChanged(SelectionChangedEvent event) {
						developerSelected(((IStructuredSelection) event
								.getSelection()).getFirstElement());
					}
				});
		Combo comboDeveloper = comboDeveloperViewer.getCombo();
		comboDeveloper.setForeground(SWTResourceManager
				.getColor(SWT.COLOR_BLACK));
		comboDeveloper.setToolTipText("Select your Developer-ID");
		GridData gd_comboDeveloper = new GridData(SWT.LEFT, SWT.CENTER, true,
				false, 2, 1);
		gd_comboDeveloper.widthHint = 127;
		comboDeveloper.setLayoutData(gd_comboDeveloper);

		tabFolder = new TabFolder(container, SWT.NONE);
		tabFolder.setVisible(false);
		GridData gd_tabFolder = new GridData(SWT.FILL, SWT.FILL, false, true,
				3, 1);
		gd_tabFolder.heightHint = 134;
		tabFolder.setLayoutData(gd_tabFolder);

		TabItem tbtmActualEffort = new TabItem(tabFolder, SWT.NONE);
		tbtmActualEffort.setText("&Actual effort");
		editActualEffortComposite = new EditActualEffortComposite(tabFolder,
				SWT.NONE, projects, selectedDeveloper);
		tbtmActualEffort.setControl(editActualEffortComposite);

		TabItem tbtmEstimatedEffort = new TabItem(tabFolder, SWT.NONE);
		tbtmEstimatedEffort.setText("&Estimated effort");
		editEstimatedEffortComposite = new EditEstimatedEffortComposite(
				tabFolder, SWT.NONE, projects.getAllProjectDevelopers(),
				selectedDeveloper);
		tbtmEstimatedEffort.setControl(editEstimatedEffortComposite);

		return container;
	}

	/**
	 * Create contents of the button bar.
	 * 
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		Button button = createButton(parent, IDialogConstants.OK_ID,
				IDialogConstants.OK_LABEL, true);
		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				f_composite.update();
				f_composite.layout(true, true);
			}
		});
		button.setText("&OK");
		m_bindingContext = initDataBindings();
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(600, 400);
	}

	private void developerSelected(Object developer) {
		this.selectedDeveloper = (Developer) developer;
		if (developer != null) {
			tabFolder.setVisible(true);
		}
		editActualEffortComposite.setDeveloper(selectedDeveloper);
		editEstimatedEffortComposite.setDeveloper(selectedDeveloper);
	}

	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		ObservableListContentProvider listContentProvider_1 = new ObservableListContentProvider();
		comboDeveloperViewer.setContentProvider(listContentProvider_1);
		//
		IObservableMap observeMap = BeansObservables
				.observeMap(listContentProvider_1.getKnownElements(),
						Developer.class, "id");
		comboDeveloperViewer.setLabelProvider(new ObservableMapLabelProvider(
				observeMap));
		//
		IObservableList projectsAllDevelopersObserveList = PojoObservables
				.observeList(Realm.getDefault(), projects, "allDevelopers");
		comboDeveloperViewer.setInput(projectsAllDevelopersObserveList);
		//
		return bindingContext;
	}
}
